﻿(function () {
    'use strict';
    var serviceId = 'httpRequestSvc';

    angular.module("common").factory(serviceId,['$http', '$q', httpRequestSvc]);
        
    function httpRequestSvc($http, $q) {

        var promiseObj = function (url, method, data, sessionKey) {
            var deferred = $q.defer();

            $http({
                method: method,
                url: url,
                data: data,
                headers: { "X-SessionKey": sessionKey },
                contentType: "application/json",
            }).success(function (reponse) {
                deferred.resolve(reponse);
            }).error(function () {
                deferred.reject("An error occured while fetching data");
            });

            return deferred.promise;
        };

        return {
            getJson: function (url, sessionKey) {
                return promiseObj(url, "GET", sessionKey);
            },
            postJson: function (url, data, sessionKey) {
                return promiseObj(url, "POST", data, sessionKey);
            },
            putJson: function (url, data, sessionKey) {
                return promiseObj(url, "PUT", data, sessionKey);
            }
        };

    }
})();
